home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / rmail.el < prev    next >
Lisp/Scheme  |  1993-06-30  |  76KB  |  2,066 lines

  1. ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs.
  2.  
  3. ;; Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: mail
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Code:
  25.  
  26. ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
  27. ;;   New features include attribute and keyword support, message
  28. ;;   selection by dispatch table, summary by attributes and keywords,
  29. ;;   expunging by dispatch table, sticky options for file commands.
  30.  
  31. ;; Extended by Bob Weiner of Motorola
  32. ;;   New features include: rmail and rmail-summary buffers remain
  33. ;;   synchronized and key bindings basically operate the same way in both
  34. ;;   buffers, summary by topic or by regular expression, rmail-reply-prefix
  35. ;;   variable, and a bury rmail buffer (wipe) command.
  36. ;;
  37.  
  38. (require 'mail-utils)
  39.  
  40. ;; For Emacs V18 compatibility
  41. (and (not (fboundp 'user-original-login-name))
  42.      (fboundp 'user-real-login-name)
  43.      (defalias 'user-original-login-name 'user-real-login-name))
  44. (and (not (fboundp 'buffer-disable-undo))
  45.      (fboundp 'buffer-flush-undo)
  46.      (defalias 'buffer-disable-undo 'buffer-flush-undo))
  47.  
  48. ; These variables now declared in paths.el.
  49. ;(defvar rmail-spool-directory "/usr/spool/mail/"
  50. ;  "This is the name of the directory used by the system mailer for\n\
  51. ;delivering new mail.  It's name should end with a slash.")
  52. ;(defvar rmail-file-name
  53. ;  (expand-file-name "~/RMAIL")
  54. ;  "")
  55.  
  56. ;;;###autoload
  57. (defvar rmail-dont-reply-to-names nil "\
  58. *A regexp specifying names to prune of reply to messages.
  59. A value of nil means exclude your own name only.")
  60.  
  61. ;;;###autoload
  62. (defvar rmail-default-dont-reply-to-names "info-" "\
  63. A regular expression specifying part of the value of the default value of
  64. the variable `rmail-dont-reply-to-names', for when the user does not set
  65. `rmail-dont-reply-to-names' explicitly.  (The other part of the default
  66. value is the user's name.)
  67. It is useful to set this variable in the site customization file.")
  68.  
  69. ;;;###autoload
  70. (defvar rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^received:\\|^message-id:\\|^summary-line:" "\
  71. *Gubbish headers one would rather not see.")
  72.  
  73. ;;;###autoload
  74. (defvar rmail-delete-after-output nil "\
  75. *Non-nil means automatically delete a message that is copied to a file.")
  76.  
  77. ;;;###autoload
  78. (defvar rmail-primary-inbox-list nil "\
  79. *List of files which are inboxes for user's primary mail file `~/RMAIL'.
  80. `nil' means the default, which is (\"/usr/spool/mail/$USER\")
  81. \(the name varies depending on the operating system,
  82. and the value of the environment variable MAIL overrides it).")
  83.  
  84. ;; These may be altered by site-init.el to match the format of mmdf files
  85. ;;  delimiting used on a given host (delim1 and delim2 from the config
  86. ;;  files).
  87.  
  88. (defvar mmdf-delim1 "^\001\001\001\001\n"
  89.   "Regexp marking the start of an mmdf message")
  90. (defvar mmdf-delim2 "^\001\001\001\001\n"
  91.   "Regexp marking the end of an mmdf message")
  92.  
  93. (defvar rmail-message-filter nil
  94.   "If non nil, is a filter function for new headers in RMAIL.
  95. Called with region narrowed to unformatted header.")
  96.  
  97. (defvar rmail-reply-prefix "Re: "
  98.   "String to prepend to Subject line when replying to a message.")
  99.  
  100. (defvar rmail-mode-map nil)
  101.  
  102. (defvar rmail-inbox-list nil)
  103. (defvar rmail-keywords nil)
  104.  
  105. ;; Message counters and markers.  Deleted flags.
  106.  
  107. (defvar rmail-current-message nil)
  108. (defvar rmail-total-messages nil)
  109. (defvar rmail-message-vector nil)
  110. (defvar rmail-deleted-vector nil)
  111.  
  112. ;; These are used by autoloaded rmail-summary.
  113.  
  114. (defvar rmail-summary-buffer nil)
  115. (defvar rmail-summary-vector nil)
  116.  
  117. ;; `Sticky' default variables.
  118.  
  119. ;; Last individual label specified to a or k.
  120. (defvar rmail-last-label nil)
  121. ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
  122. (defvar rmail-last-multi-labels nil)
  123. (defvar rmail-last-file nil)
  124. (defvar rmail-last-regexp nil)
  125. (defvar rmail-last-rmail-file nil)
  126.  
  127. ;;; Regexp matching the delimiter of messages in UNIX mail format
  128. ;;; (UNIX From lines), minus the initial ^.  Note that if you change
  129. ;;; this expression, you must change the code in rmail-nuke-pinhead-header
  130. ;;; that knows the exact ordering of the \\( \\) subexpressions.
  131. (defvar rmail-unix-mail-delimiter
  132.   (let ((time-zone-regexp
  133.      (concat "\\([A-Z]?[A-Z][A-Z][A-Z]\\( DST\\)?"
  134.          "\\|[-+]?[0-9][0-9][0-9][0-9]"
  135.          "\\|"
  136.          "\\) *")))
  137.     (concat
  138.      "From "
  139.  
  140.      ;; Username, perhaps with a quoted section that can contain spaces.
  141.      "\\("
  142.      "[^ \n]*"
  143.      "\\(\\|\".*\"[^ \n]*\\)"
  144.      "\\)  ?"
  145.  
  146.      ;; The time the message was sent.
  147.      "\\([^ \n]*\\) *"            ; day of the week
  148.      "\\([^ ]*\\) *"            ; month
  149.      "\\([0-9]*\\) *"            ; day of month
  150.      "\\([0-9:]*\\) *"            ; time of day
  151.  
  152.      ;; Perhaps a time zone, specified by an abbreviation, or by a
  153.      ;; numeric offset.
  154.      time-zone-regexp
  155.  
  156.      ;; The year.
  157.      " [0-9][0-9]\\([0-9]*\\) *"
  158.  
  159.      ;; On some systems the time zone can appear after the year, too.
  160.      time-zone-regexp
  161.  
  162.      ;; I'm not sure what this is.
  163.      "\\(remote from [^\n]*\\)?"
  164.  
  165.      "\n"))
  166.   nil)
  167.  
  168. ;; Perform BODY in the summary buffer
  169. ;; in such a way that its cursor is properly updated in its own window.
  170. (defmacro rmail-select-summary (&rest body)
  171.   (` (progn (if (rmail-summary-displayed)
  172.         (let ((window (selected-window)))
  173.           (save-excursion
  174.             (unwind-protect
  175.             (progn
  176.               (pop-to-buffer rmail-summary-buffer)
  177.               (,@ body))
  178.               (select-window window))))
  179.           (save-excursion
  180.         (set-buffer rmail-summary-buffer)
  181.         (progn (,@ body))))
  182.         (rmail-maybe-display-summary))))
  183.  
  184. ;;;; *** Rmail Mode ***
  185.  
  186. ;;;###autoload
  187. (defun rmail (&optional file-name-arg)
  188.   "Read and edit incoming mail.
  189. Moves messages into file named by  rmail-file-name  (a babyl format file)
  190.  and edits that file in RMAIL Mode.
  191. Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
  192.  
  193. May be called with filename as argument; then performs rmail editing on
  194. that file, but does not copy any new mail into the file."
  195.   (interactive (if current-prefix-arg
  196.            (list (read-file-name "Run rmail on RMAIL file: "
  197.                      nil nil t))))
  198.   (or rmail-last-file
  199.       (setq rmail-last-file (expand-file-name "~/xmail")))
  200.   (or rmail-last-rmail-file
  201.       (setq rmail-last-rmail-file (expand-file-name "~/XMAIL")))
  202.   (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
  203.      (existed (get-file-buffer file-name)))
  204.     ;; Like find-file, but in the case where a buffer existed
  205.     ;; and the file was reverted, recompute the message-data.
  206.     (if (and existed (not (verify-visited-file-modtime existed)))
  207.     (progn
  208.       ;; Don't be confused by apparent local-variables spec
  209.       ;; in the last message in the RMAIL file.
  210.       (let ((enable-local-variables nil))
  211.         (find-file file-name))
  212.       (if (and (verify-visited-file-modtime existed)
  213.            (eq major-mode 'rmail-mode))
  214.           (progn (rmail-forget-messages)
  215.              (rmail-set-message-counters))))
  216.       (let ((enable-local-variables nil))
  217.     (find-file file-name)))
  218.     (if (eq major-mode 'rmail-edit-mode)
  219.     (error "Exit Rmail Edit mode before getting new mail."))
  220.     (if (and existed (> (buffer-size) 0))
  221.     ;; Buffer not new and not empty; ensure in proper mode, but that's all.
  222.     (or (eq major-mode 'rmail-mode)
  223.         (rmail-mode-2))
  224.       (rmail-mode-2)
  225.       ;; Convert all or part to Babyl file if possible.
  226.       (rmail-convert-file)
  227.       (goto-char (point-max))
  228.       (if (null rmail-inbox-list)
  229.       (progn
  230.         (rmail-set-message-counters)
  231.         (rmail-show-message))))
  232.     (let ((existing-unseen (rmail-first-unseen-message)))
  233.       (rmail-get-new-mail)
  234.       ;; Show the first unseen message, which might be from a previous session
  235.       ;; or might have been just read in by rmail-get-new-mail.  Must
  236.       ;; determine already unseen messages first, as rmail-get-new-mail
  237.       ;; positions on the first new message, thus marking it as seen.
  238.       (rmail-show-message existing-unseen))))
  239.  
  240. ;; Given the value of MAILPATH, return a list of inbox file names.
  241. ;; This is turned off because it is not clear that the user wants
  242. ;; all these inboxes to feed into the primary rmail file.
  243. ; (defun rmail-convert-mailpath (string)
  244. ;   (let (idx list)
  245. ;     (while (setq idx (string-match "[%:]" string))
  246. ;       (let ((this (substring string 0 idx)))
  247. ;     (setq string (substring string (1+ idx)))
  248. ;     (setq list (cons (if (string-match "%" this)
  249. ;                  (substring this 0 (string-match "%" this))
  250. ;                this)
  251. ;              list))))
  252. ;     list))
  253.  
  254. ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
  255. ; will not cause emacs 18.55 problems.
  256.  
  257. (defun rmail-convert-file ()
  258.   (let (convert)
  259.     (widen)
  260.     (goto-char (point-min))
  261.     ;; If file doesn't start like a Babyl file,
  262.     ;; convert it to one, by adding a header and converting each message.
  263.     (cond ((looking-at "BABYL OPTIONS:"))
  264.       ((looking-at "Version: 5\n")
  265.        ;; Losing babyl file made by old version of Rmail.
  266.        ;; Just fix the babyl file header; don't make a new one,
  267.        ;; so we don't lose the Labels: file attribute, etc.
  268.        (let ((buffer-read-only nil))
  269.          (insert "BABYL OPTIONS: -*- rmail -*-\n")))
  270.       (t
  271.        (setq convert t)
  272.        (rmail-insert-rmail-file-header)))
  273.     ;; If file was not a Babyl file or if there are
  274.     ;; Unix format messages added at the end,
  275.     ;; convert file as necessary.
  276.     (if (or convert
  277.         (progn (goto-char (point-max))
  278.            (search-backward "\^_")
  279.            (forward-char 1)
  280.            (looking-at "\n*From ")))
  281.     (let ((buffer-read-only nil))
  282.       (message "Converting to Babyl format...")
  283.       (narrow-to-region (point) (point-max))
  284.       (rmail-convert-to-babyl-format)
  285.       (message "Converting to Babyl format...done")))))
  286.  
  287. ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
  288. ; will not cause emacs 18.55 problems.
  289.  
  290. (defun rmail-insert-rmail-file-header ()
  291.   (let ((buffer-read-only nil))
  292.     (insert "BABYL OPTIONS: -*- rmail -*-
  293. Version: 5
  294. Labels:
  295. Note:   This is the header of an rmail file.
  296. Note:   If you are seeing it in rmail,
  297. Note:    it means the file has no messages in it.\n\^_")))
  298.  
  299. (if rmail-mode-map
  300.     nil
  301.   (setq rmail-mode-map (make-keymap))
  302.   (suppress-keymap rmail-mode-map)
  303.   (define-key rmail-mode-map "a"      'rmail-add-label)
  304.   (define-key rmail-mode-map "b"      'rmail-bury)
  305.   (define-key rmail-mode-map "c"      'rmail-continue)
  306.   (define-key rmail-mode-map "d"      'rmail-delete-forward)
  307.   (define-key rmail-mode-map "\C-d"   'rmail-delete-backward)
  308.   (define-key rmail-mode-map "e"      'rmail-edit-current-message)
  309.   (define-key rmail-mode-map "f"      'rmail-forward)
  310.   (define-key rmail-mode-map "g"      'rmail-get-new-mail)
  311.   (define-key rmail-mode-map "h"      'rmail-summary)
  312.   (define-key rmail-mode-map "i"      'rmail-input)
  313.   (define-key rmail-mode-map "j"      'rmail-show-message)
  314.   (define-key rmail-mode-map "k"      'rmail-kill-label)
  315.   (define-key rmail-mode-map "l"      'rmail-summary-by-labels)
  316.   (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
  317.   (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
  318.   (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
  319.   (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
  320.   (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
  321.   (define-key rmail-mode-map "m"      'rmail-mail)
  322.   (define-key rmail-mode-map "\em"    'rmail-retry-failure)
  323.   (define-key rmail-mode-map "n"      'rmail-next-undeleted-message)
  324.   (define-key rmail-mode-map "\en"    'rmail-next-message)
  325.   (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
  326.   (define-key rmail-mode-map "o"      'rmail-output-to-rmail-file)
  327.   (define-key rmail-mode-map "\C-o"   'rmail-output)
  328.   (define-key rmail-mode-map "p"      'rmail-previous-undeleted-message)
  329.   (define-key rmail-mode-map "\ep"    'rmail-previous-message)
  330.   (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
  331.   (define-key rmail-mode-map "q"      'rmail-quit)
  332.   (define-key rmail-mode-map "r"      'rmail-reply)
  333. ;; I find I can't live without the default M-r command -- rms.
  334. ;;  (define-key rmail-mode-map "\er"  'rmail-search-backwards)
  335.   (define-key rmail-mode-map "s"      'rmail-expunge-and-save)
  336.   (define-key rmail-mode-map "\es"    'rmail-search)
  337.   (define-key rmail-mode-map "t"      'rmail-toggle-header)
  338.   (define-key rmail-mode-map "u"      'rmail-undelete-previous-message)
  339.   (define-key rmail-mode-map "w"      'rmail-edit-current-message)
  340.   (define-key rmail-mode-map "x"      'rmail-expunge)
  341.   (define-key rmail-mode-map "."      'rmail-beginning-of-message)
  342.   (define-key rmail-mode-map "<"      'rmail-first-message)
  343.   (define-key rmail-mode-map ">"      'rmail-last-message)
  344.   (define-key rmail-mode-map " "      'scroll-up)
  345.   (define-key rmail-mode-map "\177"   'scroll-down)
  346.   (define-key rmail-mode-map "?"      'describe-mode)
  347.   (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
  348.   (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
  349.   (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
  350.   (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
  351.   (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
  352.   (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
  353.   )
  354.  
  355. (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
  356.  
  357. (define-key rmail-mode-map [menu-bar classify]
  358.   (cons "Classify" (make-sparse-keymap "Classify")))
  359.  
  360. (define-key rmail-mode-map [menu-bar classify output-inbox]
  361.   '("Output (inbox)" . rmail-output))
  362.  
  363. (define-key rmail-mode-map [menu-bar classify output]
  364.   '("Output (Rmail)" . rmail-output-to-rmail-file))
  365.  
  366. (define-key rmail-mode-map [menu-bar classify kill-label]
  367.   '("Kill Label" . rmail-kill-label))
  368.  
  369. (define-key rmail-mode-map [menu-bar classify add-label]
  370.   '("Add Label" . rmail-add-label))
  371.  
  372. (define-key rmail-mode-map [menu-bar summary]
  373.   (cons "Summary" (make-sparse-keymap "Summary")))
  374.  
  375. (define-key rmail-mode-map [menu-bar summary labels]
  376.   '("By Labels" . rmail-summary-by-labels))
  377.  
  378. (define-key rmail-mode-map [menu-bar summary recipients]
  379.   '("By Recipients" . rmail-summary-by-recipients))
  380.  
  381. (define-key rmail-mode-map [menu-bar summary topic]
  382.   '("By Topic" . rmail-summary-by-topic))
  383.  
  384. (define-key rmail-mode-map [menu-bar summary regexp]
  385.   '("By Regexp" . rmail-summary-by-regexp))
  386.  
  387. (define-key rmail-mode-map [menu-bar summary all]
  388.   '("All" . rmail-summary))
  389.  
  390. (define-key rmail-mode-map [menu-bar mail]
  391.   (cons "Mail" (make-sparse-keymap "Mail")))
  392.  
  393. (define-key rmail-mode-map [menu-bar mail continue]
  394.   '("Continue" . rmail-continue))
  395.  
  396. (define-key rmail-mode-map [menu-bar mail forward]
  397.   '("Forward" . rmail-forward))
  398.  
  399. (define-key rmail-mode-map [menu-bar mail retry]
  400.   '("Retry" . rmail-retry-failure))
  401.  
  402. (define-key rmail-mode-map [menu-bar mail reply]
  403.   '("Reply" . rmail-reply))
  404.  
  405. (define-key rmail-mode-map [menu-bar mail mail]
  406.   '("Mail" . rmail-mail))
  407.  
  408. (define-key rmail-mode-map [menu-bar delete]
  409.   (cons "Delete" (make-sparse-keymap "Delete")))
  410.  
  411. (define-key rmail-mode-map [menu-bar delete expunge/save]
  412.   '("Expunge/Save" . rmail-expunge-and-save))
  413.  
  414. (define-key rmail-mode-map [menu-bar delete expunge]
  415.   '("Expunge" . rmail-expunge))
  416.  
  417. (define-key rmail-mode-map [menu-bar delete undelete]
  418.   '("Undelete" . rmail-undelete-previous-message))
  419.  
  420. (define-key rmail-mode-map [menu-bar delete delete]
  421.   '("Delete" . rmail-delete-forward))
  422.  
  423. (define-key rmail-mode-map [menu-bar move]
  424.   (cons "Move" (make-sparse-keymap "Move")))
  425.  
  426. (define-key rmail-mode-map [menu-bar move search-back]
  427.   '("Search Back" . rmail-search-backward))
  428.  
  429. (define-key rmail-mode-map [menu-bar move search]
  430.   '("Search" . rmail-search))
  431.  
  432. (define-key rmail-mode-map [menu-bar move previous]
  433.   '("Previous Nondeleted" . rmail-previous-undeleted-message))
  434.  
  435. (define-key rmail-mode-map [menu-bar move next]
  436.   '("Next Nondeleted" . rmail-next-undeleted-message))
  437.  
  438. (define-key rmail-mode-map [menu-bar move last]
  439.   '("Last" . rmail-last-message))
  440.  
  441. (define-key rmail-mode-map [menu-bar move first]
  442.   '("First" . rmail-first-message))
  443.  
  444. (define-key rmail-mode-map [menu-bar move previous]
  445.   '("Previous" . rmail-previous-message))
  446.  
  447. (define-key rmail-mode-map [menu-bar move next]
  448.   '("Next" . rmail-next-message))
  449.  
  450. ;; Rmail mode is suitable only for specially formatted data.
  451. (put 'rmail-mode 'mode-class 'special)
  452.  
  453. ;;;###autoload
  454. (defun rmail-mode ()
  455.   "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
  456. All normal editing commands are turned off.
  457. Instead, these commands are available:
  458.  
  459. \\[rmail-beginning-of-message]    Move point to front of this message (same as \\[beginning-of-buffer]).
  460. \\[scroll-up]    Scroll to next screen of this message.
  461. \\[scroll-down]    Scroll to previous screen of this message.
  462. \\[rmail-next-undeleted-message]    Move to Next non-deleted message.
  463. \\[rmail-previous-undeleted-message]    Move to Previous non-deleted message.
  464. \\[rmail-next-message]    Move to Next message whether deleted or not.
  465. \\[rmail-previous-message]    Move to Previous message whether deleted or not.
  466. \\[rmail-first-message]    Move to the first message in Rmail file.
  467. \\[rmail-last-message]    Move to the last message in Rmail file.
  468. \\[rmail-show-message]    Jump to message specified by numeric position in file.
  469. \\[rmail-search]    Search for string and show message it is found in.
  470. \\[rmail-delete-forward]    Delete this message, move to next nondeleted.
  471. \\[rmail-delete-backward]    Delete this message, move to previous nondeleted.
  472. \\[rmail-undelete-previous-message]    Undelete message.  Tries current message, then earlier messages
  473.     till a deleted message is found.
  474. \\[rmail-edit-current-message]    Edit the current message.  \\[rmail-cease-edit] to return to Rmail.
  475. \\[rmail-expunge]    Expunge deleted messages.
  476. \\[rmail-expunge-and-save]    Expunge and save the file.
  477. \\[rmail-quit]       Quit Rmail: expunge, save, then switch to another buffer.
  478. \\[save-buffer] Save without expunging.
  479. \\[rmail-get-new-mail]    Move new mail from system spool directory into this file.
  480. \\[rmail-mail]    Mail a message (same as \\[mail-other-window]).
  481. \\[rmail-continue]    Continue composing outgoing message started before.
  482. \\[rmail-reply]    Reply to this message.  Like \\[rmail-mail] but initializes some fields.
  483. \\[rmail-retry-failure]    Send this message again.  Used on a mailer failure message.
  484. \\[rmail-forward]    Forward this message to another user.
  485. \\[rmail-output-to-rmail-file]       Output this message to an Rmail file (append it).
  486. \\[rmail-output]    Output this message to a Unix-format mail file (append it).
  487. \\[rmail-input]    Input Rmail file.  Run Rmail on that file.
  488. \\[rmail-add-label]    Add label to message.  It will be displayed in the mode line.
  489. \\[rmail-kill-label]    Kill label.  Remove a label from current message.
  490. \\[rmail-next-labeled-message]   Move to Next message with specified label
  491.           (label defaults to last one specified).
  492.           Standard labels: filed, unseen, answered, forwarded, deleted.
  493.           Any other label is present only if you add it with \\[rmail-add-label].
  494. \\[rmail-previous-labeled-message]   Move to Previous message with specified label
  495. \\[rmail-summary]    Show headers buffer, with a one line summary of each message.
  496. \\[rmail-summary-by-labels]    Summarize only messages with particular label(s).
  497. \\[rmail-summary-by-recipients]   Summarize only messages with particular recipient(s).
  498. \\[rmail-summary-by-regexp]   Summarize only messages with particular regexp(s).
  499. \\[rmail-summary-by-topic]   Summarize only messages with subject line regexp(s).
  500. \\[rmail-toggle-header]    Toggle display of complete header."
  501.   (interactive)
  502.   (rmail-mode-2)
  503.   (rmail-set-message-counters)
  504.   (rmail-show-message rmail-total-messages))
  505.  
  506. (defun rmail-mode-2 ()
  507.   (kill-all-local-variables)
  508.   (rmail-mode-1)
  509.   (rmail-variables)
  510.   (run-hooks 'rmail-mode-hook))
  511.  
  512. (defun rmail-mode-1 ()
  513.   (setq major-mode 'rmail-mode)
  514.   (setq mode-name "RMAIL")
  515.   (setq buffer-read-only t)
  516.   ;; No need to auto save RMAIL files.
  517.   (setq buffer-auto-save-file-name nil)
  518.   (if (boundp 'mode-line-modified)
  519.       (setq mode-line-modified "--- ")
  520.     (setq mode-line-format
  521.       (cons "--- " (cdr (default-value 'mode-line-format)))))
  522.   (use-local-map rmail-mode-map)
  523.   (set-syntax-table text-mode-syntax-table)
  524.   (setq local-abbrev-table text-mode-abbrev-table))
  525.  
  526. (defun rmail-variables ()
  527.   (make-local-variable 'revert-buffer-function)
  528.   (setq revert-buffer-function 'rmail-revert)
  529.   (make-local-variable 'rmail-last-label)
  530.   (make-local-variable 'rmail-last-regexp)
  531.   (make-local-variable 'rmail-deleted-vector)
  532.   (make-local-variable 'rmail-summary-buffer)
  533.   (make-local-variable 'rmail-summary-vector)
  534.   (make-local-variable 'rmail-current-message)
  535.   (make-local-variable 'rmail-total-messages)
  536.   (make-local-variable 'require-final-newline)
  537.   (setq require-final-newline nil)
  538.   (make-local-variable 'version-control)
  539.   (setq version-control 'never)
  540.   (make-local-variable 'file-precious-flag)
  541.   (setq file-precious-flag t)
  542.   (make-local-variable 'rmail-message-vector)
  543.   (make-local-variable 'rmail-last-file)
  544.   (make-local-variable 'rmail-inbox-list)
  545.   (setq rmail-inbox-list (rmail-parse-file-inboxes))
  546.   ;; Provide default set of inboxes for primary mail file ~/RMAIL.
  547.   (and (null rmail-inbox-list)
  548.        (equal buffer-file-name (expand-file-name rmail-file-name))
  549.        (setq rmail-inbox-list
  550.          (or rmail-primary-inbox-list
  551.          (list (or (getenv "MAIL")
  552.                (concat rmail-spool-directory
  553.                    (user-original-login-name)))))))
  554.   (make-local-variable 'rmail-keywords)
  555.   ;; this gets generated as needed
  556.   (setq rmail-keywords nil))
  557.  
  558. ;; Handle M-x revert-buffer done in an rmail-mode buffer.
  559. (defun rmail-revert (arg noconfirm)
  560.   (let (revert-buffer-function)
  561.     ;; Call our caller again, but this time it does the default thing.
  562.     (if (revert-buffer arg noconfirm)
  563.     ;; If the user said "yes", and we changed something,
  564.     ;; reparse the messages.
  565.     (progn
  566.       (rmail-convert-file)
  567.       (goto-char (point-max))
  568.       (rmail-set-message-counters)
  569.       (rmail-show-message)))))
  570.  
  571. ;; Return a list of files from this buffer's Mail: option.
  572. ;; Does not assume that messages have been parsed.
  573. ;; Just returns nil if buffer does not look like Babyl format.
  574. (defun rmail-parse-file-inboxes ()
  575.   (save-excursion
  576.     (save-restriction
  577.       (widen)
  578.       (goto-char 1)
  579.       (cond ((looking-at "BABYL OPTIONS:")
  580.          (search-forward "\n\^_" nil 'move)
  581.          (narrow-to-region 1 (point))
  582.          (goto-char 1)
  583.          (if (search-forward "\nMail:" nil t)
  584.          (progn
  585.            (narrow-to-region (point) (progn (end-of-line) (point)))
  586.            (goto-char (point-min))
  587.            (mail-parse-comma-list))))))))
  588.  
  589. (defun rmail-expunge-and-save ()
  590.   "Expunge and save RMAIL file."
  591.   (interactive)
  592.   (rmail-expunge)
  593.   (save-buffer))
  594.  
  595. (defun rmail-quit ()
  596.   "Quit out of RMAIL."
  597.   (interactive)
  598.   (rmail-expunge-and-save)
  599.   ;; Don't switch to the summary buffer even if it was recently visible.
  600.   (if rmail-summary-buffer
  601.       (bury-buffer rmail-summary-buffer))
  602.   (let ((obuf (current-buffer)))
  603.     (switch-to-buffer (other-buffer))
  604.     (bury-buffer obuf)))
  605.  
  606. ;;;###autoload
  607. (defun rmail-input (filename)
  608.   "Run Rmail on file FILENAME."
  609.   (interactive "FRun rmail on RMAIL file: ")
  610.   (rmail filename))
  611.  
  612.  
  613. ;;;; *** Rmail input ***
  614.  
  615. ;; RLK feature not added in this version:
  616. ;; argument specifies inbox file or files in various ways.
  617.  
  618. (defun rmail-get-new-mail (&optional file-name)
  619.   "Move any new mail from this RMAIL file's inbox files.
  620. The inbox files can be specified with the file's Mail: option.  The
  621. variable `rmail-primary-inbox-list' specifies the inboxes for your
  622. primary RMAIL file if it has no Mail: option.  By default, this is
  623. your /usr/spool/mail/$USER.
  624.  
  625. You can also specify the file to get new mail from.  In this case, the
  626. file of new mail is not changed or deleted.  Noninteractively, you can
  627. pass the inbox file name as an argument.  Interactively, a prefix
  628. argument causes us to read a file name and use that file as the inbox."
  629.   (interactive
  630.    (list (if current-prefix-arg
  631.          (read-file-name "Get new mail from file: "))))
  632.   (or (verify-visited-file-modtime (current-buffer))
  633.       (progn
  634.     (find-file (buffer-file-name))
  635.     (setq buffer-read-only t)
  636.     (if (verify-visited-file-modtime (current-buffer))
  637.         (rmail-forget-messages))))
  638.   (rmail-maybe-set-message-counters)
  639.   (widen)
  640.   ;; Get rid of all undo records for this buffer.
  641.   (or (eq buffer-undo-list t)
  642.       (setq buffer-undo-list nil))
  643.   (unwind-protect
  644.       (let ((opoint (point))
  645.         (new-messages 0)
  646.         (delete-files ())
  647.         ;; If buffer has not changed yet, and has not been saved yet,
  648.         ;; don't replace the old backup file now.
  649.         (make-backup-files (and make-backup-files (buffer-modified-p)))
  650.         (buffer-read-only nil)
  651.         ;; Don't make undo records for what we do in getting mail.
  652.         (buffer-undo-list t))
  653.     (goto-char (point-max))
  654.     (skip-chars-backward " \t\n")        ; just in case of brain damage
  655.     (delete-region (point) (point-max)) ; caused by require-final-newline
  656.     (save-excursion
  657.       (save-restriction
  658.         (narrow-to-region (point) (point))
  659.         ;; Read in the contents of the inbox files,
  660.         ;; renaming them as necessary,
  661.         ;; and adding to the list of files to delete eventually.
  662.         (if file-name
  663.         (rmail-insert-inbox-text (list file-name) nil)
  664.           (setq delete-files (rmail-insert-inbox-text rmail-inbox-list t)))
  665.         ;; Scan the new text and convert each message to babyl format.
  666.         (goto-char (point-min))
  667.         (save-excursion
  668.           (setq new-messages (rmail-convert-to-babyl-format)))
  669.         (or (zerop new-messages)
  670.         (let (success)
  671.           (widen)
  672.           (search-backward "\n\^_")
  673.           (narrow-to-region (point) (point-max))
  674.           (goto-char (1+ (point-min)))
  675.           (rmail-count-new-messages)
  676.           (save-buffer)))
  677.         ;; Delete the old files, now that babyl file is saved.
  678.         (while delete-files
  679.           (condition-case ()
  680.           ;; First, try deleting.
  681.           (condition-case ()
  682.               (delete-file (car delete-files))
  683.             (file-error
  684.              ;; If we can't delete it, truncate it.
  685.              (write-region (point) (point) (car delete-files))))
  686.         (file-error nil))
  687.           (setq delete-files (cdr delete-files)))))
  688.     (if (= new-messages 0)
  689.         (progn (goto-char opoint)
  690.            (if (or file-name rmail-inbox-list)
  691.                (message "(No new mail has arrived)")))
  692.       (if (rmail-summary-exists)
  693.           (rmail-select-summary
  694.         (rmail-update-summary)))
  695.       (message "%d new message%s read"
  696.            new-messages (if (= 1 new-messages) "" "s"))
  697.       (and (boundp 'display-time-string)
  698.            (string-match " Mail" display-time-string)
  699.            (setq display-time-string
  700.              (concat
  701.               (substring display-time-string 0 (match-beginning 0))
  702.               (substring display-time-string (match-end 0))))
  703.            (force-mode-line-update 'all))))
  704.     ;; Don't leave the buffer screwed up if we get a disk-full error.
  705.     (rmail-show-message)))
  706.  
  707. (defun rmail-insert-inbox-text (files renamep)
  708.   (let (file tofile delete-files movemail popmail)
  709.     (while files
  710.       (setq file (expand-file-name (substitute-in-file-name (car files)))
  711.         ;;>> un*x specific <<
  712.         ;; The "+" used to be "~", which is an extremely poor choice;
  713.         ;; it might accidentally be deleted when space is low
  714.         ;; (as happened to me!).
  715.         tofile (concat file "+"))
  716.       ;; If getting from mail spool directory,
  717.       ;; use movemail to move rather than just renaming,
  718.       ;; so as to interlock with the mailer.
  719.       (setq movemail (equal (file-name-directory file) rmail-spool-directory)
  720.         popmail (string-match "^po:" (file-name-nondirectory file)))
  721.       (if popmail (setq file (file-name-nondirectory file)
  722.             renamep t))
  723.       (if movemail
  724.       (progn
  725.         (setq tofile (expand-file-name
  726.                ;; Generate name to move to from inbox name,
  727.                ;; in case of multiple inboxes that need moving.
  728.                (concat ".newmail-" (file-name-nondirectory file))
  729.                ;; Use the directory of this rmail file
  730.                ;; because it's a nuisance to use the homedir
  731.                ;; if that is on a full disk and this rmail
  732.                ;; file isn't.
  733.                (file-name-directory
  734.                  (expand-file-name buffer-file-name))))
  735.         ;; On some systems, /usr/spool/mail/foo is a directory
  736.         ;; and the actual inbox is /usr/spool/mail/foo/foo.
  737.         (if (file-directory-p file)
  738.         (setq file (expand-file-name (user-original-login-name)
  739.                          file)))))
  740.       (if popmail
  741.       (message "Getting mail from post office ...")
  742.     (if (or (and (file-exists-p tofile)
  743.              (/= 0 (nth 7 (file-attributes tofile))))
  744.         (and (file-exists-p file)
  745.              (/= 0 (nth 7 (file-attributes file)))))
  746.         (message "Getting mail from %s..." file)))
  747.       ;; Set TOFILE if have not already done so, and
  748.       ;; rename or copy the file FILE to TOFILE if and as appropriate.
  749.       (cond ((not renamep)
  750.          (setq tofile file))
  751.         ((or (file-exists-p tofile) (and (not popmail)
  752.                          (not (file-exists-p file))))
  753.          nil)
  754.         ((and (not movemail) (not popmail))
  755.          (rename-file file tofile nil)
  756.          ;; Make the real inbox file empty.
  757.          ;; Leaving it deleted could cause lossage
  758.          ;; because mailers often won't create the file.
  759.          (condition-case ()
  760.          (write-region (point) (point) file)
  761.            (file-error nil)))
  762.         (t
  763.          (let ((errors nil))
  764.            (unwind-protect
  765.            (save-excursion
  766.              (setq errors (generate-new-buffer " *rmail loss*"))
  767.              (buffer-disable-undo errors)
  768.              (call-process
  769.                (expand-file-name "movemail" exec-directory)
  770.                nil errors nil file tofile)
  771.              (if (not (buffer-modified-p errors))
  772.              ;; No output => movemail won
  773.              nil
  774.                (set-buffer errors)
  775.                (subst-char-in-region (point-min) (point-max)
  776.                          ?\n ?\  )
  777.                (goto-char (point-max))
  778.                (skip-chars-backward " \t")
  779.                (delete-region (point) (point-max))
  780.                (goto-char (point-min))
  781.                (if (looking-at "movemail: ")
  782.                (delete-region (point-min) (match-end 0)))
  783.                (beep t)
  784.                (message (concat "movemail: "
  785.                     (buffer-substring (point-min)
  786.                               (point-max))))
  787.                (sit-for 3)
  788.                nil))
  789.          (if errors (kill-buffer errors))))))
  790.       ;; At this point, TOFILE contains the name to read:
  791.       ;; Either the alternate name (if we renamed)
  792.       ;; or the actual inbox (if not renaming).
  793.       (if (file-exists-p tofile)
  794.       (let (size)
  795.         (goto-char (point-max))
  796.         (setq size (nth 1 (insert-file-contents tofile)))
  797.         (goto-char (point-max))
  798.         (or (= (preceding-char) ?\n)
  799.         (zerop size)
  800.         (insert ?\n))
  801.         (setq delete-files (cons tofile delete-files))))
  802.       (message "")
  803.       (setq files (cdr files)))
  804.     delete-files))
  805.  
  806. ;; the  rmail-break-forwarded-messages  feature is not implemented
  807. (defun rmail-convert-to-babyl-format ()
  808.   (let ((count 0) start
  809.     (case-fold-search nil)
  810.     (invalid-input-resync
  811.      (function (lambda ()
  812.              (message "Invalid Babyl format in inbox!")
  813.              (sit-for 1)
  814.              ;; Try to get back in sync with a real message.
  815.              (if (re-search-forward
  816.               (concat mmdf-delim1 "\\|^From") nil t)
  817.              (beginning-of-line)
  818.                (goto-char (point-max)))))))
  819.     (goto-char (point-min))
  820.     (save-restriction
  821.       (while (not (eobp))
  822.     (cond ((looking-at "BABYL OPTIONS:");Babyl header
  823.            (if (search-forward "\n\^_" nil t)
  824.            ;; If we find the proper terminator, delete through there.
  825.            (delete-region (point-min) (point))
  826.          (funcall invalid-input-resync)
  827.          (delete-region (point-min) (point))))
  828.           ;; Babyl format message
  829.           ((looking-at "\^L")
  830.            (or (search-forward "\n\^_" nil t)
  831.            (funcall invalid-input-resync))
  832.            (setq count (1+ count))
  833.            ;; Make sure there is no extra white space after the ^_
  834.            ;; at the end of the message.
  835.            ;; Narrowing will make sure that whatever follows the junk
  836.            ;; will be treated properly.
  837.            (delete-region (point)
  838.                   (save-excursion
  839.                 (skip-chars-forward " \t\n")
  840.                 (point)))
  841.            (narrow-to-region (point) (point-max)))
  842.           ;;*** MMDF format
  843.           ((let ((case-fold-search t))
  844.          (looking-at mmdf-delim1))
  845.            (let ((case-fold-search t))
  846.          (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
  847.          (setq start (point))
  848.          (re-search-forward mmdf-delim2 nil t)
  849.          (replace-match "\^_"))
  850.            (save-excursion
  851.          (save-restriction
  852.            (narrow-to-region start (1- (point)))
  853.            (goto-char (point-min))
  854.            (while (search-forward "\n\^_" nil t); single char "\^_"
  855.              (replace-match "\n^_")))); 2 chars: "^" and "_"
  856.            (narrow-to-region (point) (point-max))
  857.            (setq count (1+ count)))
  858.           ;;*** Mail format
  859.           ((looking-at "^From ")
  860.            (setq start (point))
  861.            (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
  862.            (rmail-nuke-pinhead-header)
  863.            ;; If this message has a Content-Length field,
  864.            ;; skip to the end of the contents.
  865.            (let* ((header-end (save-excursion
  866.                     (and (re-search-forward "\n\n" nil t)
  867.                      (point))))
  868.               (case-fold-search t)
  869.               (size
  870.                ;; Get the numeric value from the Content-Length field.
  871.                (save-excursion
  872.              ;; Back up to end of prev line,
  873.              ;; in case the Content-Length field comes first.
  874.              (forward-char -1)
  875.              (and (search-forward "\ncontent-length: "
  876.                           header-end t)
  877.                   (let ((beg (point))
  878.                     (eol (progn (end-of-line) (point))))
  879.                 (read (buffer-substring beg eol)))))))
  880.          (if size
  881.              (goto-char (+ header-end size))))
  882.  
  883.            (if (re-search-forward
  884.             (concat "^[\^_]?\\("
  885.                 rmail-unix-mail-delimiter
  886.                 "\\|"
  887.                 mmdf-delim1 "\\|"
  888.                 "^BABYL OPTIONS:\\|"
  889.                 "\^L\n[01],\\)") nil t)
  890.            (goto-char (match-beginning 1))
  891.          (goto-char (point-max)))
  892.            (setq count (1+ count))
  893.            (save-excursion
  894.          (save-restriction
  895.            (narrow-to-region start (point))
  896.            (goto-char (point-min))
  897.            (while (search-forward "\n\^_" nil t); single char
  898.              (replace-match "\n^_")))); 2 chars: "^" and "_"
  899.            (insert ?\^_)
  900.            (narrow-to-region (point) (point-max)))
  901.           ;;
  902.           ;; This kludge is because some versions of sendmail.el
  903.           ;; insert an extra newline at the beginning that shouldn't
  904.           ;; be there.  sendmail.el has been fixed, but old versions
  905.           ;; may still be in use.  -- rms, 7 May 1993.
  906.           ((eolp) (delete-char 1))
  907.           (t (error "Cannot convert to babyl format")))))
  908.     count))
  909.  
  910. ;; Delete the "From ..." line, creating various other headers with
  911. ;; information from it if they don't already exist.  Now puts the
  912. ;; original line into a mail-from: header line for debugging.
  913. (defun rmail-nuke-pinhead-header ()
  914.   (save-excursion
  915.     (save-restriction
  916.       (let ((start (point))
  917.           (end (progn
  918.            (condition-case ()
  919.                (search-forward "\n\n")
  920.              (error
  921.               (goto-char (point-max))
  922.               (insert "\n\n")))
  923.            (point)))
  924.         has-from has-date)
  925.     (narrow-to-region start end)
  926.     (let ((case-fold-search t))
  927.       (goto-char start)
  928.       (setq has-from (search-forward "\nFrom:" nil t))
  929.       (goto-char start)
  930.       (setq has-date (and (search-forward "\nDate:" nil t) (point)))
  931.       (goto-char start))
  932.     (let ((case-fold-search nil))
  933.       (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
  934.           (replace-match
  935.         (concat
  936.           "Mail-from: \\&"
  937.           ;; Keep and reformat the date if we don't
  938.           ;;  have a Date: field.
  939.           (if has-date
  940.               ""
  941.             (concat
  942.              "Date: \\3, \\5 \\4 \\9 \\6 "
  943.             
  944.              ;; The timezone could be matched by group 7 or group 10.
  945.              ;; If neither of them matched, assume EST, since only
  946.              ;; Easterners would be so sloppy.
  947.              ;; It's a shame the substitution can't use "\\10".
  948.              (cond
  949.               ((/= (match-beginning 7) (match-end 7)) "\\7")
  950.               ((/= (match-beginning 10) (match-end 10))
  951.                (buffer-substring (match-beginning 10)
  952.                      (match-end 10)))
  953.               (t "EST"))
  954.              "\n"))
  955.           ;; Keep and reformat the sender if we don't
  956.           ;; have a From: field.
  957.           (if has-from
  958.               ""
  959.             "From: \\1\n")))))))))
  960.  
  961. ;;;; *** Rmail Message Formatting and Header Manipulation ***
  962.  
  963. (defun rmail-reformat-message (beg end)
  964.   (goto-char beg)
  965.   (forward-line 1)
  966.   (if (/= (following-char) ?0)
  967.       (error "Bad format in RMAIL file."))
  968.   (let ((buffer-read-only nil)
  969.     (delta (- (buffer-size) end)))
  970.     (delete-char 1)
  971.     (insert ?1)
  972.     (forward-line 1)
  973.     (if (looking-at "Summary-line: ")
  974.     (forward-line 1))
  975.     (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
  976.     (delete-region (point)
  977.                (progn (forward-line 1) (point))))
  978.     (let ((str (buffer-substring (point)
  979.                  (save-excursion (search-forward "\n\n" end 'move)
  980.                          (point)))))
  981.       (insert str "*** EOOH ***\n")
  982.       (narrow-to-region (point) (- (buffer-size) delta)))
  983.     (goto-char (point-min))
  984.     (if rmail-ignored-headers (rmail-clear-headers))
  985.     (if rmail-message-filter (funcall rmail-message-filter))))
  986.  
  987. (defun rmail-clear-headers ()
  988.   (if (search-forward "\n\n" nil t)
  989.       (save-restriction
  990.         (narrow-to-region (point-min) (point))
  991.     (let ((buffer-read-only nil))
  992.       (while (let ((case-fold-search t))
  993.            (goto-char (point-min))
  994.            (re-search-forward rmail-ignored-headers nil t))
  995.         (beginning-of-line)
  996.         (delete-region (point)
  997.                (progn (re-search-forward "\n[^ \t]")
  998.                   (forward-char -1)
  999.                   (point))))))))
  1000.  
  1001. (defun rmail-toggle-header ()
  1002.   "Show original message header if pruned header currently shown, or vice versa."
  1003.   (interactive)
  1004.   (rmail-maybe-set-message-counters)
  1005.   (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
  1006.   (let ((buffer-read-only nil))
  1007.     (goto-char (point-min))
  1008.     (forward-line 1)
  1009.     (if (= (following-char) ?1)
  1010.     (progn (delete-char 1)
  1011.            (insert ?0)
  1012.            (forward-line 1)
  1013.            (if (looking-at "Summary-Line:")
  1014.            (forward-line 1))
  1015.            (insert "*** EOOH ***\n")
  1016.            (forward-char -1)
  1017.            (search-forward "\n*** EOOH ***\n")
  1018.            (forward-line -1)
  1019.            (let ((temp (point)))
  1020.          (and (search-forward "\n\n" nil t)
  1021.               (delete-region temp (point))))
  1022.            (goto-char (point-min))
  1023.            (search-forward "\n*** EOOH ***\n")
  1024.            (narrow-to-region (point) (point-max)))
  1025.       (rmail-reformat-message (point-min) (point-max)))))
  1026.  
  1027. ;;;; *** Rmail Attributes and Keywords ***
  1028.  
  1029. ;; Make a string describing current message's attributes and keywords
  1030. ;; and set it up as the name of a minor mode
  1031. ;; so it will appear in the mode line.
  1032. (defun rmail-display-labels ()
  1033.   (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
  1034.     (save-excursion
  1035.       (unwind-protect
  1036.       (progn
  1037.         (widen)
  1038.         (goto-char (rmail-msgbeg rmail-current-message))
  1039.         (forward-line 1)
  1040.         (if (looking-at "[01],")
  1041.         (progn
  1042.           (narrow-to-region (point) (progn (end-of-line) (point)))
  1043.           ;; Truly valid BABYL format requires a space before each
  1044.           ;; attribute or keyword name.  Put them in if missing.
  1045.           (let (buffer-read-only)
  1046.             (goto-char (point-min))
  1047.             (while (search-forward "," nil t)
  1048.               (or (looking-at "[ ,]") (eobp)
  1049.               (insert " "))))
  1050.           (goto-char (point-max))
  1051.           (if (search-backward ",," nil 'move)
  1052.               (progn
  1053.             (if (> (point) (1+ (point-min)))
  1054.                 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
  1055.             (if (> (- (point-max) (point)) 2)
  1056.                 (setq blurb
  1057.                   (concat blurb
  1058.                       ";"
  1059.                       (buffer-substring (+ (point) 3)
  1060.                                 (1- (point-max)))))))))))
  1061.     ;; Note: we don't use save-restriction because that does not work right
  1062.     ;; if changes are made outside the saved restriction
  1063.     ;; before that restriction is restored.
  1064.     (narrow-to-region beg end)
  1065.     (set-marker beg nil)
  1066.     (set-marker end nil)))
  1067.     (while (string-match " +," blurb)
  1068.       (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
  1069.               (substring blurb (match-end 0)))))
  1070.     (while (string-match ", +" blurb)
  1071.       (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
  1072.               (substring blurb (match-end 0)))))
  1073.     (setq mode-line-process
  1074.       (concat " " rmail-current-message "/" rmail-total-messages
  1075.           blurb))))
  1076.  
  1077. ;; Turn an attribute of a message on or off according to STATE.
  1078. ;; ATTR is the name of the attribute, as a string.
  1079. ;; MSGNUM is message number to change; nil means current message.
  1080. (defun rmail-set-attribute (attr state &optional msgnum)
  1081.   (let ((omax (point-max-marker))
  1082.     (omin (point-min-marker))
  1083.     (buffer-read-only nil))
  1084.     (or msgnum (setq msgnum rmail-current-message))
  1085.     (unwind-protect
  1086.     (save-excursion
  1087.       (widen)
  1088.       (goto-char (+ 3 (rmail-msgbeg msgnum)))
  1089.       (let ((curstate
  1090.          (not
  1091.           (null (search-backward (concat ", " attr ",")
  1092.                      (prog1 (point) (end-of-line)) t)))))
  1093.         (or (eq curstate (not (not state)))
  1094.         (if curstate
  1095.             (delete-region (point) (1- (match-end 0)))
  1096.           (beginning-of-line)
  1097.           (forward-char 2)
  1098.           (insert " " attr ","))))
  1099.       (if (string= attr "deleted")
  1100.           (rmail-set-message-deleted-p msgnum state)))
  1101.       ;; Note: we don't use save-restriction because that does not work right
  1102.       ;; if changes are made outside the saved restriction
  1103.       ;; before that restriction is restored.
  1104.       (narrow-to-region omin omax)
  1105.       (set-marker omin nil)
  1106.       (set-marker omax nil)
  1107.       (if (= msgnum rmail-current-message)
  1108.       (rmail-display-labels)))))
  1109.  
  1110. ;; Return t if the attributes/keywords line of msg number MSG
  1111. ;; contains a match for the regexp LABELS.
  1112. (defun rmail-message-labels-p (msg labels)
  1113.   (save-excursion
  1114.     (save-restriction
  1115.       (widen)
  1116.       (goto-char (rmail-msgbeg msg))
  1117.       (forward-char 3)
  1118.       (re-search-backward labels (prog1 (point) (end-of-line)) t))))
  1119.  
  1120. ;;;; *** Rmail Message Selection And Support ***
  1121.  
  1122. (defun rmail-msgend (n)
  1123.   (marker-position (aref rmail-message-vector (1+ n))))
  1124.  
  1125. (defun rmail-msgbeg (n)
  1126.   (marker-position (aref rmail-message-vector n)))
  1127.  
  1128. (defun rmail-widen-to-current-msgbeg (function)
  1129.   "Call FUNCTION with point at start of internal data of current message.
  1130. Assumes that bounds were previously narrowed to display the message in Rmail.
  1131. The bounds are widened enough to move point where desired, then narrowed
  1132. again afterward.
  1133.  
  1134. FUNCTION may not change the visible text of the message, but it may
  1135. change the invisible header text."
  1136.   (save-excursion
  1137.     (let ((obeg (- (point-max) (point-min))))
  1138.       (unwind-protect
  1139.       (progn
  1140.         (narrow-to-region (rmail-msgbeg rmail-current-message)
  1141.                   (point-max))
  1142.         (goto-char (point-min))
  1143.         (funcall function))
  1144.     ;; Note: we don't use save-restriction because that does not work right
  1145.     ;; if changes are made outside the saved restriction
  1146.     ;; before that restriction is restored.
  1147.     ;; Here we assume that changes made by FUNCTION
  1148.     ;; occur before the visible region of the message.
  1149.     (narrow-to-region (- (point-max) obeg) (point-max))))))
  1150.  
  1151. (defun rmail-forget-messages ()
  1152.   (unwind-protect
  1153.       (if (vectorp rmail-message-vector)
  1154.       (let* ((i 0)
  1155.          (v rmail-message-vector)
  1156.          (n (length v)))
  1157.         (while (< i n)
  1158.           (move-marker (aref v i)  nil)
  1159.           (setq i (1+ i)))))
  1160.     (setq rmail-message-vector nil)
  1161.     (setq rmail-deleted-vector nil)))
  1162.  
  1163. (defun rmail-maybe-set-message-counters ()
  1164.   (if (not (and rmail-deleted-vector
  1165.         rmail-message-vector
  1166.         rmail-current-message
  1167.         rmail-total-messages))
  1168.       (rmail-set-message-counters)))
  1169.  
  1170. (defun rmail-count-new-messages (&optional nomsg)
  1171.   (let* ((case-fold-search nil)
  1172.      (total-messages 0)
  1173.      (messages-head nil)
  1174.      (deleted-head nil))
  1175.     (or nomsg (message "Counting new messages..."))
  1176.     (goto-char (point-max))
  1177.     ;; Put at the end of messages-head
  1178.     ;; the entry for message N+1, which marks
  1179.     ;; the end of message N.  (N = number of messages).
  1180.     (search-backward "\n\^_")
  1181.     (forward-char 1)
  1182.     (setq messages-head (list (point-marker)))
  1183.     (rmail-set-message-counters-counter (point-min))
  1184.     (setq rmail-current-message (1+ rmail-total-messages))
  1185.     (setq rmail-total-messages
  1186.       (+ rmail-total-messages total-messages))
  1187.     (setq rmail-message-vector
  1188.       (vconcat rmail-message-vector (cdr messages-head)))
  1189.     (aset rmail-message-vector
  1190.       rmail-current-message (car messages-head))
  1191.     (setq rmail-deleted-vector
  1192.       (concat rmail-deleted-vector deleted-head))
  1193.     (setq rmail-summary-vector
  1194.       (vconcat rmail-summary-vector (make-vector total-messages nil)))
  1195.     (goto-char (point-min))
  1196.     (or nomsg (message "Counting new messages...done (%d)" total-messages))))
  1197.  
  1198. (defun rmail-set-message-counters ()
  1199.   (rmail-forget-messages)
  1200.   (save-excursion
  1201.     (save-restriction
  1202.       (widen)
  1203.       (let* ((point-save (point))
  1204.          (total-messages 0)
  1205.          (messages-after-point)
  1206.          (case-fold-search nil)
  1207.          (messages-head nil)
  1208.          (deleted-head nil))
  1209.     (message "Counting messages...")
  1210.     (goto-char (point-max))
  1211.     ;; Put at the end of messages-head
  1212.     ;; the entry for message N+1, which marks
  1213.     ;; the end of message N.  (N = number of messages).
  1214.     (search-backward "\n\^_")
  1215.     (forward-char 1)
  1216.     (setq messages-head (list (point-marker)))
  1217.     (rmail-set-message-counters-counter (min (point) point-save))
  1218.     (setq messages-after-point total-messages)
  1219.     (rmail-set-message-counters-counter)
  1220.     (setq rmail-total-messages total-messages)
  1221.     (setq rmail-current-message
  1222.           (min total-messages
  1223.            (max 1 (- total-messages messages-after-point))))
  1224.     (setq rmail-message-vector
  1225.           (apply 'vector (cons (point-min-marker) messages-head))
  1226.           rmail-deleted-vector (concat "D" deleted-head)
  1227.           rmail-summary-vector (make-vector rmail-total-messages nil))
  1228.     (message "Counting messages...done")))))
  1229.     
  1230. (defun rmail-set-message-counters-counter (&optional stop)
  1231.   (while (search-backward "\n\^_\^L\n" stop t)
  1232.     (forward-char 1)
  1233.     (setq messages-head (cons (point-marker) messages-head))
  1234.     (save-excursion
  1235.       (setq deleted-head
  1236.         (cons (if (search-backward ", deleted,"
  1237.                        (prog1 (point)
  1238.                      (forward-line 2))
  1239.                        t)
  1240.               ?D ?\ )
  1241.           deleted-head)))
  1242.     (if (zerop (% (setq total-messages (1+ total-messages)) 20))
  1243.     (message "Counting messages...%d" total-messages))))
  1244.  
  1245. (defun rmail-beginning-of-message ()
  1246.   "Show current message starting from the beginning."
  1247.   (interactive)
  1248.   (rmail-show-message rmail-current-message))
  1249.  
  1250. (defun rmail-show-message (&optional n)
  1251.   "Show message number N (prefix argument), counting from start of file.
  1252. If summary buffer is currently displayed, update current message there also."
  1253.   (interactive "p")
  1254.   (rmail-maybe-set-message-counters)
  1255.   (widen)
  1256.   (if (zerop rmail-total-messages)
  1257.       (progn (narrow-to-region (point-min) (1- (point-max)))
  1258.          (goto-char (point-min))
  1259.          (setq mode-line-process nil))
  1260.     (let (blurb)
  1261.       (if (not n)
  1262.       (setq n rmail-current-message)
  1263.     (cond ((<= n 0)
  1264.            (setq n 1
  1265.              rmail-current-message 1
  1266.              blurb "No previous message"))
  1267.           ((> n rmail-total-messages)
  1268.            (setq n rmail-total-messages
  1269.              rmail-current-message rmail-total-messages
  1270.              blurb "No following message"))
  1271.           (t
  1272.            (setq rmail-current-message n))))
  1273.       (let ((beg (rmail-msgbeg n))
  1274.         (end (rmail-msgend n)))
  1275.     (goto-char beg)
  1276.     (forward-line 1)
  1277.     (if (= (following-char) ?0)
  1278.         (progn
  1279.           (rmail-reformat-message beg end)
  1280.           (rmail-set-attribute "unseen" nil))
  1281.       (search-forward "\n*** EOOH ***\n" end t)
  1282.       (narrow-to-region (point) end))
  1283.     (goto-char (point-min))
  1284.     (rmail-display-labels)
  1285.     (run-hooks 'rmail-show-message-hook)
  1286.     ;; If there is a summary buffer, try to move to this message
  1287.     ;; in that buffer.  But don't complain if this message
  1288.     ;; is not mentioned in the summary.
  1289.     (if (rmail-summary-exists)
  1290.         (let ((curr-msg rmail-current-message))
  1291.           (rmail-select-summary
  1292.            (rmail-summary-goto-msg curr-msg t t))))
  1293.     (if blurb
  1294.         (message blurb))))))
  1295.  
  1296. (defun rmail-next-message (n)
  1297.   "Show following message whether deleted or not.
  1298. With prefix arg N, moves forward N messages, or backward if N is negative."
  1299.   (interactive "p")
  1300.   (rmail-maybe-set-message-counters)
  1301.   (rmail-show-message (+ rmail-current-message n)))
  1302.  
  1303. (defun rmail-previous-message (n)
  1304.   "Show previous message whether deleted or not.
  1305. With prefix arg N, moves backward N messages, or forward if N is negative."
  1306.   (interactive "p")
  1307.   (rmail-next-message (- n)))  
  1308.  
  1309. (defun rmail-next-undeleted-message (n)
  1310.   "Show following non-deleted message.
  1311. With prefix arg N, moves forward N non-deleted messages,
  1312. or backward if N is negative."
  1313.   (interactive "p")
  1314.   (rmail-maybe-set-message-counters)
  1315.   (let ((lastwin rmail-current-message)
  1316.     (current rmail-current-message))
  1317.     (while (and (> n 0) (< current rmail-total-messages))
  1318.       (setq current (1+ current))
  1319.       (if (not (rmail-message-deleted-p current))
  1320.       (setq lastwin current n (1- n))))
  1321.     (while (and (< n 0) (> current 1))
  1322.       (setq current (1- current))
  1323.       (if (not (rmail-message-deleted-p current))
  1324.       (setq lastwin current n (1+ n))))
  1325.     (if (/= lastwin rmail-current-message)
  1326.     (rmail-show-message lastwin))
  1327.     (if (< n 0)
  1328.     (message "No previous nondeleted message"))
  1329.     (if (> n 0)
  1330.     (message "No following nondeleted message"))))
  1331.  
  1332. (defun rmail-previous-undeleted-message (n)
  1333.   "Show previous non-deleted message.
  1334. With prefix argument N, moves backward N non-deleted messages,
  1335. or forward if N is negative."
  1336.   (interactive "p")
  1337.   (rmail-next-undeleted-message (- n)))
  1338.  
  1339. (defun rmail-first-message ()
  1340.   "Show first message in file."
  1341.   (interactive)
  1342.   (rmail-maybe-set-message-counters)
  1343.   (rmail-show-message 1))
  1344.  
  1345. (defun rmail-last-message ()
  1346.   "Show last message in file."
  1347.   (interactive)
  1348.   (rmail-maybe-set-message-counters)
  1349.   (rmail-show-message rmail-total-messages))
  1350.  
  1351. (defun rmail-what-message ()
  1352.   (let ((where (point))
  1353.     (low 1)
  1354.     (high rmail-total-messages)
  1355.     (mid (/ rmail-total-messages 2)))
  1356.     (while (> (- high low) 1)
  1357.       (if (>= where (rmail-msgbeg mid))
  1358.       (setq low mid)
  1359.     (setq high mid))
  1360.       (setq mid (+ low (/ (- high low) 2))))
  1361.     (if (>= where (rmail-msgbeg high)) high low)))
  1362.  
  1363. (defun rmail-message-recipients-p (msg recipients &optional primary-only)
  1364.   (save-restriction
  1365.     (goto-char (rmail-msgbeg msg))
  1366.     (search-forward "\n*** EOOH ***\n")
  1367.     (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
  1368.     (or (string-match recipients (or (mail-fetch-field "To") ""))
  1369.     (string-match recipients (or (mail-fetch-field "From") ""))
  1370.     (if (not primary-only)
  1371.         (string-match recipients (or (mail-fetch-field "Cc") ""))))))
  1372.  
  1373. (defun rmail-message-regexp-p (msg regexp)
  1374.   "Return t, if for message number MSG, regexp REGEXP matches in the header."
  1375.   (goto-char (rmail-msgbeg msg))
  1376.   (let ((end 
  1377.          (save-excursion 
  1378.            (search-forward "*** EOOH ***" (point-max)) (point))))
  1379.     (re-search-forward regexp end t)))
  1380.  
  1381. (defun rmail-search-backward (regexp &optional n)
  1382.   "Show message containing next match for REGEXP.
  1383. Prefix argument gives repeat count; negative argument means search
  1384. backwards (through earlier messages).
  1385. Interactively, empty argument means use same regexp used last time."
  1386.   (interactive
  1387.     (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
  1388.        (prompt
  1389.         (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1390.        regexp)
  1391.       (if rmail-search-last-regexp
  1392.       (setq prompt (concat prompt
  1393.                    "(default "
  1394.                    rmail-search-last-regexp
  1395.                    ") ")))
  1396.       (setq regexp (read-string prompt))
  1397.       (cond ((not (equal regexp ""))
  1398.          (setq rmail-search-last-regexp regexp))
  1399.         ((not rmail-search-last-regexp)
  1400.          (error "No previous Rmail search string")))
  1401.       (list rmail-search-last-regexp
  1402.         (prefix-numeric-value current-prefix-arg))))
  1403.   (rmail-search regexp (- n)))
  1404.  
  1405. (defvar rmail-search-last-regexp nil)
  1406. (defun rmail-search (regexp &optional n)
  1407.   "Show message containing next match for REGEXP.
  1408. Prefix argument gives repeat count; negative argument means search
  1409. backwards (through earlier messages).
  1410. Interactively, empty argument means use same regexp used last time."
  1411.   (interactive
  1412.     (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
  1413.        (prompt
  1414.         (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1415.        regexp)
  1416.       (if rmail-search-last-regexp
  1417.       (setq prompt (concat prompt
  1418.                    "(default "
  1419.                    rmail-search-last-regexp
  1420.                    ") ")))
  1421.       (setq regexp (read-string prompt))
  1422.       (cond ((not (equal regexp ""))
  1423.          (setq rmail-search-last-regexp regexp))
  1424.         ((not rmail-search-last-regexp)
  1425.          (error "No previous Rmail search string")))
  1426.       (list rmail-search-last-regexp
  1427.         (prefix-numeric-value current-prefix-arg))))
  1428.   (or n (setq n 1))
  1429.   (message "%sRmail search for %s..."
  1430.        (if (< n 0) "Reverse " "")
  1431.        regexp)
  1432.   (rmail-maybe-set-message-counters)
  1433.   (let ((omin (point-min))
  1434.     (omax (point-max))
  1435.     (opoint (point))
  1436.     win
  1437.     (reversep (< n 0))
  1438.     (msg rmail-current-message))
  1439.     (unwind-protect
  1440.     (progn
  1441.       (widen)
  1442.       (while (/= n 0)
  1443.         ;; Check messages one by one, advancing message number up or down
  1444.         ;; but searching forward through each message.
  1445.         (if reversep
  1446.         (while (and (null win) (> msg 1))
  1447.           (goto-char (rmail-msgbeg (setq msg (1- msg))))
  1448.           (setq win (re-search-forward
  1449.                  regexp (rmail-msgend msg) t)))
  1450.           (while (and (null win) (< msg rmail-total-messages))
  1451.         (goto-char (rmail-msgbeg (setq msg (1+ msg))))
  1452.         (setq win (re-search-forward regexp (rmail-msgend msg) t))))
  1453.         (setq n (+ n (if reversep 1 -1)))))
  1454.       (if win
  1455.       (progn
  1456.         ;; If this is a reverse search and we found a message,
  1457.         ;; search backward thru this message to position point.
  1458.         (if reversep
  1459.         (progn
  1460.           (goto-char (rmail-msgend msg))
  1461.           (re-search-backward
  1462.            regexp (rmail-msgbeg msg) t)))
  1463.         (setq win (point))
  1464.         (rmail-show-message msg)
  1465.         (message "%sRmail search for %s...done"
  1466.              (if reversep "Reverse " "")
  1467.              regexp)
  1468.         (goto-char win))
  1469.     (goto-char opoint)
  1470.     (narrow-to-region omin omax)
  1471.     (ding)
  1472.     (message "Search failed: %s" regexp)))))
  1473.  
  1474. (defun rmail-search-backwards (regexp &optional n)
  1475.   "Show message containing previous match for REGEXP.
  1476. Prefix argument gives repeat count; negative argument means search
  1477. forward (through later messages).
  1478. Interactively, empty argument means use same regexp used last time."
  1479.   (interactive
  1480.     (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
  1481.        (prompt
  1482.         (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1483.        regexp)
  1484.       (if rmail-search-last-regexp
  1485.       (setq prompt (concat prompt
  1486.                    "(default "
  1487.                    rmail-search-last-regexp
  1488.                    ") ")))
  1489.       (setq regexp (read-string prompt))
  1490.       (cond ((not (equal regexp ""))
  1491.          (setq rmail-search-last-regexp regexp))
  1492.         ((not rmail-search-last-regexp)
  1493.          (error "No previous Rmail search string")))
  1494.       (list rmail-search-last-regexp
  1495.         (prefix-numeric-value current-prefix-arg))))
  1496.   (rmail-search regexp (- (or n -1))))
  1497.  
  1498. ;; Show the first message which has the `unseen' attribute.
  1499. (defun rmail-first-unseen-message ()
  1500.   (rmail-maybe-set-message-counters)
  1501.   (let ((current 1)
  1502.     found)
  1503.     (save-restriction
  1504.       (widen)
  1505.       (while (and (not found) (< current rmail-total-messages))
  1506.     (if (rmail-message-labels-p current ", ?\\(unseen\\),")
  1507.         (setq found current))
  1508.     (setq current (1+ current))))
  1509. ;; Let the caller show the message.
  1510. ;;    (if found
  1511. ;;    (rmail-show-message found))
  1512.     found))
  1513.  
  1514. ;;;; *** Rmail Message Deletion Commands ***
  1515.  
  1516. (defun rmail-message-deleted-p (n)
  1517.   (= (aref rmail-deleted-vector n) ?D))
  1518.  
  1519. (defun rmail-set-message-deleted-p (n state)
  1520.   (aset rmail-deleted-vector n (if state ?D ?\ )))
  1521.  
  1522. (defun rmail-delete-message ()
  1523.   "Delete this message and stay on it."
  1524.   (interactive)
  1525.   (rmail-set-attribute "deleted" t))
  1526.  
  1527. (defun rmail-undelete-previous-message ()
  1528.   "Back up to deleted message, select it, and undelete it."
  1529.   (interactive)
  1530.   (let ((msg rmail-current-message))
  1531.     (while (and (> msg 0)
  1532.         (not (rmail-message-deleted-p msg)))
  1533.       (setq msg (1- msg)))
  1534.     (if (= msg 0)
  1535.     (error "No previous deleted message")
  1536.       (if (/= msg rmail-current-message)
  1537.       (rmail-show-message msg))
  1538.       (rmail-set-attribute "deleted" nil)
  1539.       (if (rmail-summary-exists)
  1540.       (save-excursion
  1541.         (set-buffer rmail-summary-buffer)
  1542.         (rmail-summary-mark-undeleted msg)))
  1543.       (rmail-maybe-display-summary))))
  1544.  
  1545. (defun rmail-delete-forward (&optional backward)
  1546.   "Delete this message and move to next nondeleted one.
  1547. Deleted messages stay in the file until the \\[rmail-expunge] command is given.
  1548. With prefix argument, delete and move backward."
  1549.   (interactive "P")
  1550.   (rmail-set-attribute "deleted" t)
  1551.   (let ((del-msg rmail-current-message))
  1552.     (if (rmail-summary-exists)
  1553.     (save-excursion
  1554.       (set-buffer rmail-summary-buffer)
  1555.       (rmail-summary-mark-deleted del-msg)))
  1556.     (rmail-next-undeleted-message (if backward -1 1))
  1557.     (rmail-maybe-display-summary)))
  1558.  
  1559. (defun rmail-delete-backward ()
  1560.   "Delete this message and move to previous nondeleted one.
  1561. Deleted messages stay in the file until the \\[rmail-expunge] command is given."
  1562.   (interactive)
  1563.   (rmail-delete-forward t))
  1564.  
  1565. (defun rmail-only-expunge ()
  1566.   "Actually erase all deleted messages in the file."
  1567.   (interactive)
  1568.   (message "Expunging deleted messages...")
  1569.   ;; Discard all undo records for this buffer.
  1570.   (or (eq buffer-undo-list t)
  1571.       (setq buffer-undo-list nil))
  1572.   (rmail-maybe-set-message-counters)
  1573.   (let* ((omax (- (buffer-size) (point-max)))
  1574.      (omin (- (buffer-size) (point-min)))
  1575.      (opoint (if (and (> rmail-current-message 0)
  1576.               (= ?D (aref rmail-deleted-vector rmail-current-message)))
  1577.              0 (- (point) (point-min))))
  1578.      (messages-head (cons (aref rmail-message-vector 0) nil))
  1579.      (messages-tail messages-head)
  1580.      ;; Don't make any undo records for the expunging.
  1581.      (buffer-undo-list t)
  1582.      (win))
  1583.     (unwind-protect
  1584.     (save-excursion
  1585.       (widen)
  1586.       (goto-char (point-min))
  1587.       (let ((counter 0)
  1588.         (number 1)
  1589.         (total rmail-total-messages)
  1590.         (new-message-number rmail-current-message)
  1591.         (new-summary nil)
  1592.         (buffer-read-only nil)
  1593.         (messages rmail-message-vector)
  1594.         (deleted rmail-deleted-vector)
  1595.         (summary rmail-summary-vector))
  1596.         (setq rmail-total-messages nil
  1597.           rmail-current-message nil
  1598.           rmail-message-vector nil
  1599.           rmail-deleted-vector nil
  1600.           rmail-summary-vector nil)
  1601.         (while (<= number total)
  1602.           (if (= (aref deleted number) ?D)
  1603.           (progn
  1604.             (delete-region
  1605.               (marker-position (aref messages number))
  1606.               (marker-position (aref messages (1+ number))))
  1607.             (move-marker (aref messages number) nil)
  1608.             (if (> new-message-number counter)
  1609.             (setq new-message-number (1- new-message-number))))
  1610.         (setq counter (1+ counter))
  1611.         (setq messages-tail
  1612.               (setcdr messages-tail
  1613.                   (cons (aref messages number) nil)))
  1614.         (setq new-summary
  1615.               (cons (if (= counter number) (aref summary (1- number)))
  1616.                 new-summary)))
  1617.           (if (zerop (% (setq number (1+ number)) 20))
  1618.           (message "Expunging deleted messages...%d" number)))
  1619.         (setq messages-tail
  1620.           (setcdr messages-tail
  1621.               (cons (aref messages number) nil)))
  1622.         (setq rmail-current-message new-message-number
  1623.           rmail-total-messages counter
  1624.           rmail-message-vector (apply 'vector messages-head)
  1625.           rmail-deleted-vector (make-string (1+ counter) ?\ )
  1626.           rmail-summary-vector (vconcat (nreverse new-summary))
  1627.           win t)))
  1628.       (message "Expunging deleted messages...done")
  1629.       (if (not win)
  1630.       (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
  1631.       (rmail-show-message
  1632.        (if (zerop rmail-current-message) 1 nil))
  1633.       (forward-char opoint))))
  1634.  
  1635. (defun rmail-expunge ()
  1636.   "Erase deleted messages from Rmail file and summary buffer."
  1637.   (interactive)
  1638.   (rmail-only-expunge)
  1639.   (if (rmail-summary-exists)
  1640.       (rmail-select-summary
  1641.     (rmail-update-summary))))
  1642.  
  1643. ;;;; *** Rmail Mailing Commands ***
  1644.  
  1645. (defun rmail-mail ()
  1646.   "Send mail in another window.
  1647. While composing the message, use \\[mail-yank-original] to yank the
  1648. original message into it."
  1649.   (interactive)
  1650.   (mail-other-window nil nil nil nil nil (current-buffer)))
  1651.  
  1652. (defun rmail-continue ()
  1653.   "Continue composing outgoing message previously being composed."
  1654.   (interactive)
  1655.   (mail-other-window t))
  1656.  
  1657. (defun rmail-reply (just-sender)
  1658.   "Reply to the current message.
  1659. Normally include CC: to all other recipients of original message;
  1660. prefix argument means ignore them.  While composing the reply,
  1661. use \\[mail-yank-original] to yank the original message into it."
  1662.   (interactive "P")
  1663.   (let (from reply-to cc subject date to message-id resent-reply-to)
  1664.     (save-excursion
  1665.       (save-restriction
  1666.     (widen)
  1667.     (goto-char (rmail-msgbeg rmail-current-message))
  1668.     (forward-line 1)
  1669.     (if (= (following-char) ?0)
  1670.         (narrow-to-region
  1671.          (progn (forward-line 2)
  1672.             (point))
  1673.          (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
  1674.                     'move)
  1675.             (point)))
  1676.       (narrow-to-region (point)
  1677.                 (progn (search-forward "\n*** EOOH ***\n")
  1678.                    (beginning-of-line) (point))))
  1679.     (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
  1680.           from (mail-fetch-field "from")
  1681.           reply-to (or resent-reply-to
  1682.                (mail-fetch-field "reply-to" nil t)
  1683.                from)
  1684.           cc (cond (just-sender nil)
  1685.                (resent-reply-to (mail-fetch-field "resent-cc" t))
  1686.                (t (mail-fetch-field "cc" nil t)))
  1687.           subject (or (and resent-reply-to
  1688.                    (mail-fetch-field "resent-subject" t))
  1689.               (mail-fetch-field "subject"))
  1690.           date (cond (resent-reply-to
  1691.               (mail-fetch-field "resent-date" t))
  1692.              ((mail-fetch-field "date")))
  1693.           to (cond (resent-reply-to
  1694.             (mail-fetch-field "resent-to" t))
  1695.                ((mail-fetch-field "to" nil t))
  1696.                ;((mail-fetch-field "apparently-to")) ack gag barf
  1697.                (t ""))
  1698.           message-id (cond (resent-reply-to
  1699.                 (mail-fetch-field "resent-message-id" t))
  1700.                    ((mail-fetch-field "message-id"))))))
  1701.     (and (stringp subject)
  1702.      (or (string-match (concat "\\`" (regexp-quote rmail-reply-prefix))
  1703.                subject)
  1704.          (setq subject (concat rmail-reply-prefix subject))))
  1705.     (mail-other-window nil
  1706.       (mail-strip-quoted-names reply-to)
  1707.       subject
  1708.       (rmail-make-in-reply-to-field from date message-id)
  1709.       (if just-sender
  1710.       nil
  1711.     (let* ((cc-list (rmail-dont-reply-to
  1712.               (mail-strip-quoted-names
  1713.                 (if (null cc) to (concat to ", " cc))))))
  1714.       (if (string= cc-list "") nil cc-list)))
  1715.       (current-buffer)
  1716.       (list (list '(lambda (buf msgnum)
  1717.              (save-excursion
  1718.                (set-buffer buf)
  1719.                (rmail-set-attribute "answered" t msgnum)))
  1720.           (current-buffer) rmail-current-message)))))
  1721.  
  1722. (defun rmail-make-in-reply-to-field (from date message-id)
  1723.   (cond ((not from)
  1724.          (if message-id
  1725.              message-id
  1726.              nil))
  1727.         (mail-use-rfc822
  1728.          (require 'rfc822)
  1729.          (let ((tem (car (rfc822-addresses from))))
  1730.            (if message-id
  1731.                (if (string-match
  1732.                     (regexp-quote (if (string-match "@[^@]*\\'" tem)
  1733.                                       (substring tem 0 (match-beginning 0))
  1734.                                       tem))
  1735.                     message-id)
  1736.                    ;; Message-ID is sufficiently informative
  1737.                    message-id
  1738.                    (concat message-id " (" tem ")"))
  1739.                ;; Use prin1 to fake RFC822 quoting
  1740.                (let ((field (prin1-to-string tem)))
  1741.                  (if date
  1742.                      (concat field "'s message of " date)
  1743.                      field)))))
  1744.         ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
  1745.                 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
  1746.            ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
  1747.            (or (string-match (concat "\\`[ \t]*\\(" bar
  1748.                                      "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
  1749.                              ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
  1750.                              from)
  1751.                (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
  1752.                                      bar "\\))[ \t]*\\'")
  1753.                              ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
  1754.                              from)))
  1755.          (let ((start (match-beginning 1))
  1756.                (end (match-end 1)))
  1757.            ;; Trim whitespace which above regexp match allows
  1758.            (while (and (< start end)
  1759.                        (memq (aref from start) '(?\t ?\ )))
  1760.              (setq start (1+ start)))
  1761.            (while (and (< start end)
  1762.                        (memq (aref from (1- end)) '(?\t ?\ )))
  1763.              (setq end (1- end)))
  1764.            (let ((field (substring from start end)))
  1765.              (if date (setq field (concat "message from " field " on " date)))
  1766.              (if message-id
  1767.                  ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
  1768.                  (concat message-id " (" field ")")
  1769.                  field))))
  1770.         (t
  1771.          ;; If we can't kludge it simply, do it correctly
  1772.          (let ((mail-use-rfc822 t))
  1773.            (rmail-make-in-reply-to-field from date message-id)))))
  1774.  
  1775. (defun rmail-forward (resend)
  1776.   "Forward the current message to another user.
  1777. With prefix argument, \"resend\" the message instead of forwarding it;
  1778. see the documentation of `rmail-resend'."
  1779.   (interactive "P")
  1780.   (if resend
  1781.       (call-interactively 'rmail-resend)
  1782.     (let ((forward-buffer (current-buffer))
  1783.       (subject (concat "["
  1784.                (let ((from (or (mail-fetch-field "From")
  1785.                        (mail-fetch-field ">From"))))
  1786.                  (if from
  1787.                  (concat (mail-strip-quoted-names from) ": ")
  1788.                    ""))
  1789.                (or (mail-fetch-field "Subject") "")
  1790.                "]")))
  1791.       ;; Turn off the usual actions for initializing the message body
  1792.       ;; because we want to get only the text from the failure message.
  1793.       (let (mail-signature mail-setup-hook)
  1794.     ;; If only one window, use it for the mail buffer.
  1795.     ;; Otherwise, use another window for the mail buffer
  1796.     ;; so that the Rmail buffer remains visible
  1797.     ;; and sending the mail will get back to it.
  1798.     (if (funcall (if (one-window-p t)
  1799.              (function mail)
  1800.                (function mail-other-window))
  1801.              nil nil subject nil nil nil
  1802.              (list (list (function (lambda (buf msgnum)
  1803.                    (save-excursion
  1804.                      (set-buffer buf)
  1805.                      (rmail-set-attribute
  1806.                        "forwarded" t msgnum))))
  1807.                  (current-buffer)
  1808.                  rmail-current-message)))
  1809.         (save-excursion
  1810.           (goto-char (point-max))
  1811.           (forward-line 1)
  1812.           (insert-buffer forward-buffer)))))))
  1813.  
  1814. (defun rmail-resend (address &optional from comment mail-alias-file)
  1815.   "Resend current message to ADDRESSES.
  1816. ADDRESSES should be a single address, a a string consisting of several
  1817. addresses separated by commas, or a list of addresses.
  1818.  
  1819. Optional FROM is the address to resend the message from, and
  1820. defaults to the username of the person redistributing the message.
  1821. Optional COMMENT is a string that will be inserted as a comment in the
  1822. resent message.
  1823. Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
  1824. typically for purposes of moderating a list."
  1825.   (interactive "sResend to: ")
  1826.   (require 'sendmail)
  1827.   (require 'mailalias)
  1828.   (if (not from) (setq from (user-login-name)))
  1829.   (let ((tembuf (generate-new-buffer " sendmail temp"))
  1830.     (mail-header-separator "")
  1831.     (case-fold-search nil)
  1832.     (mailbuf (current-buffer)))
  1833.     (unwind-protect
  1834.     (save-excursion
  1835.       ;;>> Copy message into temp buffer
  1836.       (set-buffer tembuf)
  1837.       (insert-buffer-substring mailbuf)
  1838.       (goto-char (point-min))
  1839.       ;;>> Insert resent-from:
  1840.       (insert "Resent-From: " from "\n")
  1841.       (insert "Resent-Date: " (current-time-string) "\n")
  1842.       ;;>> Insert resent-to: and bcc if need be.
  1843.       (let ((before (point)))
  1844.         (insert "Resent-To: " (if (stringp address)
  1845.                    address
  1846.                  (mapconcat 'identity address ",\n\t"))
  1847.             "\n")
  1848.         (expand-mail-aliases before (point)))
  1849.       ;;>> Set up comment, if any.
  1850.       (if (and (sequencep comment) (not (zerop (length comment))))
  1851.           (let ((before (point))
  1852.             after)
  1853.         (insert comment)
  1854.         (or (eolp) (insert "\n"))
  1855.         (setq after (point))
  1856.         (goto-char before)
  1857.         (while (< (point) after)
  1858.           (insert "Resent-Comment: ")
  1859.           (forward-line 1))))
  1860.       ;; Don't expand aliases in the destination fields
  1861.       ;; of the original message.
  1862.       (let (mail-aliases)
  1863.         (sendmail-send-it)))
  1864.       (kill-buffer tembuf))
  1865.     (rmail-set-attribute "resent" t rmail-current-message)))
  1866.  
  1867. (defvar mail-unsent-separator
  1868.   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
  1869.       "^ *---+ +Returned message +---+ *$\\|"
  1870.       "^ *---+ +Original message +---+ *$\\|"
  1871.       "^ *--+ +begin message +--+ *$\\|"
  1872.       "^|? *---+ +Message text follows: +---+ *|?$"))
  1873.  
  1874. (defun rmail-retry-failure ()
  1875.   "Edit a mail message which is based on the contents of the current message.
  1876. For a message rejected by the mail system, extract the interesting headers and
  1877. the body of the original message; otherwise copy the current message."
  1878.   (interactive)
  1879.   (require 'mail-utils)
  1880.   (let (to subj irp2 cc orig-message)
  1881.     (save-excursion
  1882.       ;; Narrow down to just the quoted original message
  1883.       (rmail-beginning-of-message)
  1884.       (let ((case-fold-search t))
  1885.     (or (re-search-forward mail-unsent-separator nil t)
  1886.         (error "Cannot parse this as a failure message")))
  1887.       (save-restriction
  1888.     (narrow-to-region (point) (point-max))
  1889.     ;; Now mail-fetch-field will get from headers of the original message,
  1890.     ;; not from the headers of the rejection.
  1891.     (setq to   (mail-fetch-field "To")
  1892.           subj (mail-fetch-field "Subject")
  1893.           irp2 (mail-fetch-field "In-reply-to")
  1894.           cc   (mail-fetch-field "Cc"))
  1895.     ;; Get the entire text (not headers) of the original message.
  1896.     (setq orig-message
  1897.           (buffer-substring
  1898.            (progn (search-forward "\n\n") (point))
  1899.            (point-max)))))
  1900.     ;; Start sending a new message; default header fields from the original.
  1901.     ;; Turn off the usual actions for initializing the message body
  1902.     ;; because we want to get only the text from the failure message.
  1903.     (let (mail-signature mail-setup-hook)
  1904.       (if (mail-other-window nil to subj irp2 cc (current-buffer))
  1905.       ;; Insert original text as initial text of new draft message.
  1906.       (progn
  1907.         (goto-char (point-max))
  1908.         (insert orig-message)
  1909.         (goto-char (point-min))
  1910.         (end-of-line))))))
  1911.  
  1912. (defun rmail-bury ()
  1913.   "Bury current Rmail buffer and its summary buffer."
  1914.   (interactive)
  1915.   (let ((rmail-buffer (current-buffer)))
  1916.     (if (rmail-summary-exists)
  1917.     (let (window)
  1918.       (while (setq window (get-buffer-window rmail-summary-buffer))
  1919.         (set-window-buffer window (other-buffer rmail-summary-buffer)))
  1920.       (bury-buffer rmail-summary-buffer)))
  1921.     (switch-to-buffer (other-buffer (current-buffer)))
  1922.     (bury-buffer rmail-buffer)))
  1923.  
  1924. (defun rmail-summary-exists ()
  1925.   "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
  1926. Non-nil value returned is the summary buffer."
  1927.   (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
  1928.        rmail-summary-buffer))
  1929.  
  1930. (defun rmail-summary-displayed ()
  1931.   "t iff in RMAIL buffer and an associated summary buffer is displayed."
  1932.   (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
  1933.  
  1934. (defvar rmail-redisplay-summary nil
  1935.   "*Non-nil means Rmail should show the summary when it changes.
  1936. This has an effect only if a summary buffer exists.")
  1937.  
  1938. ;; Put the summary buffer back on the screen, if user wants that.
  1939. (defun rmail-maybe-display-summary ()
  1940.   (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
  1941.        rmail-redisplay-summary
  1942.        (display-buffer rmail-summary-buffer)))
  1943.  
  1944. ;;;; *** Rmail Specify Inbox Files ***
  1945.  
  1946. (autoload 'set-rmail-inbox-list "rmailmsc"
  1947.   "Set the inbox list of the current RMAIL file to FILE-NAME.
  1948. This may be a list of file names separated by commas.
  1949. If FILE-NAME is empty, remove any inbox list."
  1950.   t)
  1951.  
  1952. ;;;; *** Rmail Commands for Labels ***
  1953.  
  1954. (autoload 'rmail-add-label "rmailkwd"
  1955.   "Add LABEL to labels associated with current RMAIL message.
  1956. Completion is performed over known labels when reading."
  1957.   t)
  1958.  
  1959. (autoload 'rmail-kill-label "rmailkwd"
  1960.   "Remove LABEL from labels associated with current RMAIL message.
  1961. Completion is performed over known labels when reading."
  1962.   t)
  1963.  
  1964. (autoload 'rmail-next-labeled-message "rmailkwd"
  1965.   "Show next message with LABEL.  Defaults to last label used.
  1966. With prefix argument N moves forward N messages with this label."
  1967.   t)
  1968.  
  1969. (autoload 'rmail-previous-labeled-message "rmailkwd"
  1970.   "Show previous message with LABEL.  Defaults to last label used.
  1971. With prefix argument N moves backward N messages with this label."
  1972.   t)
  1973.  
  1974. (autoload 'rmail-read-label "rmailkwd"
  1975.   "PROMPT and read with completion an Rmail message label."
  1976.   t)
  1977.  
  1978. ;;;; *** Rmail Edit Mode ***
  1979.  
  1980. (autoload 'rmail-edit-current-message "rmailedit"
  1981.   "Edit the contents of the current message"
  1982.   t)
  1983.  
  1984. ;;;; *** Rmail Sorting ***
  1985.  
  1986. (autoload 'rmail-sort-by-date "rmailsort"
  1987.   "Sort messages of current Rmail file by date.
  1988. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  1989.  
  1990. (autoload 'rmail-sort-by-subject "rmailsort"
  1991.   "Sort messages of current Rmail file by subject.
  1992. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  1993.  
  1994. (autoload 'rmail-sort-by-author "rmailsort"
  1995.   "Sort messages of current Rmail file by author.
  1996. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  1997.  
  1998. (autoload 'rmail-sort-by-recipient "rmailsort"
  1999.   "Sort messages of current Rmail file by recipient.
  2000. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  2001.  
  2002. (autoload 'rmail-sort-by-correspondent "rmailsort"
  2003.   "Sort messages of current Rmail file by other correspondent.
  2004. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  2005.  
  2006. (autoload 'rmail-sort-by-lines "rmailsort"
  2007.   "Sort messages of current Rmail file by number of lines.
  2008. If prefix argument REVERSE is non-nil, sort them in reverse order." t)
  2009.  
  2010. ;;;; *** Rmail Summary Mode ***
  2011.  
  2012. (autoload 'rmail-summary "rmailsum"
  2013.   "Display a summary of all messages, one line per message."
  2014.   t)
  2015.  
  2016. (autoload 'rmail-summary-by-labels "rmailsum"
  2017.   "Display a summary of all messages with one or more LABELS.
  2018. LABELS should be a string containing the desired labels, separated by commas."
  2019.   t)
  2020.  
  2021. (autoload 'rmail-summary-by-recipients "rmailsum"
  2022.   "Display a summary of all messages with the given RECIPIENTS.
  2023. Normally checks the To, From and Cc fields of headers; but if PRIMARY-ONLY
  2024. is non-nil (prefix arg given), only look in the To and From fields.
  2025. RECIPIENTS is a string of regexps separated by commas."
  2026.   t)
  2027.  
  2028. (autoload 'rmail-summary-by-regexp "rmailsum"
  2029.   "Display a summary of all messages according to regexp REGEXP.
  2030. If the regular expression is found in the header of the message
  2031. \(including in the date and other lines, as well as the subject line),
  2032. Emacs will list the header line in the RMAIL-summary."
  2033.   t)
  2034.  
  2035. (autoload 'rmail-summary-by-topic "rmailsum"
  2036.   "Display a summary of all messages with the given SUBJECT.
  2037. Normally checks the Subject field of headers;
  2038. but if WHOLE-MESSAGE is non-nil (prefix arg given), 
  2039.  look in the whole message.
  2040. SUBJECT is a string of regexps separated by commas."
  2041.   t)
  2042.  
  2043. ;;;; *** Rmail output messages to files ***
  2044.  
  2045. (autoload 'rmail-output-to-rmail-file "rmailout"
  2046.   "Append the current message to an Rmail file named FILE-NAME.
  2047. If the file does not exist, ask if it should be created.
  2048. If file is being visited, the message is appended to the Emacs
  2049. buffer visiting that file."
  2050.   t)
  2051.  
  2052. (autoload 'rmail-output "rmailout"
  2053.   "Append this message to Unix mail file named FILE-NAME."
  2054.   t)
  2055.  
  2056. ;;;; *** Rmail undigestification ***
  2057.  
  2058. (autoload 'undigestify-rmail-message "undigest"
  2059.   "Break up a digest message into its constituent messages.
  2060. Leaves original message, deleted, before the undigestified messages."
  2061.   t)
  2062.  
  2063. (provide 'rmail)
  2064.  
  2065. ;;; rmail.el ends here
  2066.